#include "messageHandler.h"
#include <json-c/json.h>
#include "stdShared.h"


/* 构造函数 */
MessageHandler::MessageHandler(const std::shared_ptr<StdTcpSocket> & clientSock ) : m_musicSever(clientSock)
{
    
    m_handles[REGISTER] = [this](const std::string & data){m_musicSever.handleRegisterInfo(data);};
    m_handles[LOGIN] =[this](const std::string & data){m_musicSever.handleRegisterInfo(data);};
    // m_handles[SEARCH_MUSIC] =
    // m_handles[DOWNLOAD_MUSIC] =
    //m_clientSock == nullptr;
}

void MessageHandler::handleLoginInfo(const std::string &data)
{

}


void MessageHandler::handleMessage(const std::string &data)
{
    json_object * jsonObj = json_tokener_parse(data.c_str());
    if(jsonObj == NULL)
    {
        std::cout << "解析失败" << std::endl;
        return;
    }

    /* json字符串解析成功 */
    int type = json_object_get_int(json_object_object_get(jsonObj,"type"));
    
    auto iter =  m_handles.find(type);
    if(iter != m_handles.end())
    {
        /* 执行回调函数 */
        iter->second(data);
    }
    else
    {
        /* 处理未知的消息类型 */
        std::cout << "unknoen message type." << type << std::endl;
    }


}